package fuse.LaCollaFS;

import java.io.IOException;
import java.nio.ByteBuffer;

import fuse.FuseDirEnt;
import fuse.FuseException;
import fuse.FuseMount;
import fuse.FuseStat;

import junit.framework.TestCase;

public class testLaCollaFuse extends TestCase {
	// private LaCollaFS lacollafs;

	public testLaCollaFuse() throws IOException {

		// lacollafs = new LaCollaFS("/");
	}

	public void testDirectoris() throws FuseException, IOException {
		LaCollaFS lacollafs = new LaCollaFS("/");
		FuseStat stat;
		// System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!he
		// arribat aqui?1 "+lacollafs);

		
		
		
		// provar mkdir
		lacollafs.mkdir("/a", 493);
		stat = lacollafs.getattr("/a");
		p("mode stat: " + stat.mode);
		//assertEquals(stat.mode, 493);
		// /provar getdir
		FuseDirEnt[] ll = lacollafs.getdir("/");

		assertEquals(ll[2].name, "a");
	//	assertEquals(ll[2].mode, 493);
		assertEquals(ll.length, 3);
		// provar chmod
		lacollafs.chmod("/a", 16895);
		stat = lacollafs.getattr("/a");
		p(stat.mode);
		assertEquals(stat.mode, 16895);

		// provar chown
		stat = lacollafs.getattr("/a");
		p("gid i uid... " + stat.gid + "  " + stat.uid);
		lacollafs.chown("/a", 1001, -1);
		lacollafs.chown("/a", -1, 1001);
		stat = lacollafs.getattr("/a");
		assertEquals(stat.gid, 1001);
		assertEquals(stat.uid, 1001);

		// prova rmdir

		lacollafs.rmdir("/a");
		ll = lacollafs.getdir("/");
		assertEquals(ll.length, 2);

		lacollafs.mkdir("/c", 493);
		lacollafs.mkdir("/b", 493);
		lacollafs.rename("/b/", "/c/");	

		
		try {
			lacollafs.getattr("/a");
		} catch (FuseException e) {
			assertEquals(e.getErrno(), 2);
		}

		lacollafs.close();
	}

	public void testFitxers() throws FuseException, IOException {
		LaCollaFS lacollafs = new LaCollaFS("/");
		FuseStat stat;

		lacollafs.mknod("hola.txt", 33261, 0);

		lacollafs.getattr("/hola.txt");
		FuseDirEnt[] l = lacollafs.getdir("/");

		assertEquals(l[2].name, "hola.txt@0");

		lacollafs.open("/hola.txt", 32769);
		ByteBuffer buf = ByteBuffer
				.wrap((new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }));
		buf.put((byte) 'h');
		buf.put((byte) 'h');
		buf.put((byte) 'o');
		buf.put((byte) 'o');
		buf.put((byte) 'l');
		buf.put((byte) 'l');
		buf.put((byte) 'a');
		buf.put((byte) 'a');
		lacollafs.write("/hola.txt", buf, 0);

		lacollafs.release("/hola.txt", 0);
	
		
		lacollafs.open("/hola.txt", 32768);

		ByteBuffer buf2 = ByteBuffer
				.wrap((new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }));
		lacollafs.read("/hola.txt", buf2, 0);
		lacollafs.release("/hola.txt", 0);
		for (int i = 0; i < 7; i++) {
			assertEquals(buf2.get(i), buf.get(i));
		}
		


		lacollafs.utime("/hola.txt",1, 1);
		
		
		lacollafs.link("/hola.txt", "/pep.txt");
		lacollafs.symlink("/hola.txt", "/pep1.txt");
		String s = lacollafs.readlink("/pep1.txt");
	
		
		//modificant el fitxer....
		
		lacollafs.open("/hola.txt", 32769);
		ByteBuffer buf3 = ByteBuffer
				.wrap((new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }));
		buf3.put((byte) 'a');
		buf3.put((byte) 'a');
		buf3.put((byte) 'd');
		buf3.put((byte) 'd');
		buf3.put((byte) 'e');
		buf3.put((byte) 'e');
		buf3.put((byte) 'u');
		buf3.put((byte) 'u');
		lacollafs.write("/hola.txt", buf3, 0);

	//	lacollafs.release("/hola.txt",32769);

		l = lacollafs.getdir("/");

		p(l[2].name);
		//assertEquals(l[2].name,"hola.txt@2");		
		

		p(s);

		lacollafs.close();
		
	}

	private void p(Object o) {
		System.out.println("TestLaCollaFuse: " + o);
		// TODO Auto-generated method stub

	}

}
